<template>
  <div>
    <a-button type="primary" @click="openImportWin">导入数据</a-button>
    <a-table
      ref="table"
      bordered
      size="middle"
      :rowKey="
        (record, index) => {
          return index
        }
      "
      :columns="columnsExcel"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      @change="handleTableChange"
    >
    </a-table>
    <excel-paste
      :columnsExcel="columnsExcel"
      :exportXlsTemplateUrl="exportXlsTemplateUrl"
      :exportXlsTemplateRequestUrl="exportXlsTemplateRequestUrl"
      :excelVisible="excelVisible"
      :excelConfirmLoading="excelConfirmLoading"
      @handleOkExcel="handleOkExcel"
      @handleCancelExcel="handleCancelExcel"
      :showBtu="false"
    ></excel-paste>
  </div>
</template>

<script>
import ExcelPaste from '@/components/excelPaste/ExcelPaste'
import { httpAction, getAction, postAction, postActionAsync } from '@/api/manage'

export default {
  components: {
    ExcelPaste,
  },
  data() {
    return {
      url: {
        addBatch: '/projectMaReportTarget/importMaBudgetTarget',
        getMaBudgetTarget: '/projectMaReportTarget/list',
      },
      excelVisible: false,
      dataSource: [],
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 20,
        pageSizeOptions: ['20', '50', '100'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      columnsExcel: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
          isImportExcelColumns: false,
        },
        {
          title: '主导经营体',
          align: 'center',
          dataIndex: 'mainDeptName',
        },
        {
          title: '挖潜预算目标',
          align: 'center',
          dataIndex: 'budgetTarget',
        },
        {
          title: '年份',
          align: 'center',
          dataIndex: 'year',
        },
      ],
      exportXlsTemplateUrl: 'templates/excel/时间表导入模板.xls',
      exportXlsTemplateRequestUrl: '/common/exportXlsTemplate',
      excelConfirmLoading: false,
      loading: false,
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      //TODO 筛选
      // if (Object.keys(sorter).length > 0) {
      //   this.isorter.column = sorter.field
      //   this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
      // }
      this.ipagination = pagination
      this.initData()
    },
    initData() {
      this.loading = true
      getAction(this.url.getMaBudgetTarget, {
        pageNo: this.ipagination.current,
        pageSize: this.ipagination.pageSize,
      })
        .then((res) => {
          if (res.code == 200) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          } else {
            this.$message.warning('初始化信息失败:' + res.message)
            this.ipagination.total = 0
          }
          this.loading = false
        })
        .catch((res) => {
          this.$message.warning('初始化信息失败:' + res.message)
          this.loading = false
        })
    },
    openImportWin: function () {
      this.excelVisible = true
    },
    handleOkExcel: function (data) {
      this.excelDataSource = []
      for (let index = 0; index < data.length; index++) {
        console.log(data[index])
      }
      this.excelDataSource = data
      //数据验证
      this.excelConfirmLoading = true
      postAction(this.url.addBatch, { excelVOList: data })
        .then((res) => {
          this.excelConfirmLoading = false
          if (res.code === 200) {
            this.excelVisible = false
            this.$message.success('导入成功')
            this.initData()
          } else {
            this.$message.warning('操作异常:' + res.message)
          }
        })
        .catch((res) => {
          this.excelConfirmLoading = false
          this.$message.warning('操作异常:' + res.message)
        })
    },
    handleCancelExcel: function () {
      this.excelVisible = false
      this.initData()
    },
  },
}
</script>

<style lang="less" scoped>
</style>